using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ComputaGuru.SpEAd
{
    public partial class ElementSelector : Form
    {
        private EA.Repository eaRepository = null;
        private List<ElementDescriptor> selectedElements = new List<ElementDescriptor>();
        private ElementSelectorMode selectorMode = ElementSelectorMode.LinkTo;
        private SelectionMode selectionMode = SelectionMode.MultiExtended;

        public SelectionMode SelectionMode
        {
            get { return this.selectionMode; }
            set { this.selectionMode = value; }
        }

        internal ElementSelectorMode Mode
        {
            get { return this.selectorMode; }
            set { this.selectorMode = value; }
        }

        public List<ElementDescriptor> SelectedElements
        {
            get { return this.selectedElements; }
            set { this.selectedElements = value; }
        }

        public EA.Repository EaRepository
        {
            get { return this.eaRepository; }
            set { this.eaRepository = value; }
        }

        public LinkType SelectedLinkType
        {
            get
            {
                return (LinkType)Enum.Parse(typeof(LinkType), this.selectedType.SelectedIndex.ToString());
            }
        }

        public ElementSelector()
        {
            InitializeComponent();
        }

        private void ElementSelector_Load(object sender, EventArgs e)
        {
            this.selectedType.Visible = this.Mode == ElementSelectorMode.LinkTo;
            this.selectedTypeLabel.Visible = this.selectedType.Visible;

            if (this.Mode == ElementSelectorMode.LinkTo)
            {
                this.okButton.Enabled = false;
            }

            this.elementList.SelectionMode = this.SelectionMode;
            bool showSelectButtons = (this.SelectionMode != SelectionMode.One && this.SelectionMode != SelectionMode.None);
            this.selectAll.Visible = showSelectButtons;
            this.selectNone.Visible = showSelectButtons;

            if (this.EaRepository != null)
            {
                EA.Diagram currentDiagram = this.EaRepository.GetCurrentDiagram();
                EA.DiagramObject activeElement = (EA.DiagramObject)currentDiagram.SelectedObjects.GetAt(0);
                foreach (EA.DiagramObject currentObject in currentDiagram.DiagramObjects)
                {
                    if (!(currentObject.ElementID == activeElement.ElementID))
                    {
                        EA.Element currentElement = this.EaRepository.GetElementByID(currentObject.ElementID);
                        this.elementList.Items.Add(new ElementDescriptor(currentElement));
                    }
                }
            }
        }

        private void selectAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.elementList.Items.Count; i++)
            {
                this.elementList.SetSelected(i, true);
            }
        }

        private void selectNone_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.elementList.Items.Count; i++)
            {
                this.elementList.SetSelected(i, false);
            }
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            foreach (ElementDescriptor currentElement in this.elementList.SelectedItems)
            {
                this.SelectedElements.Add(currentElement);
            }

            this.DialogResult = DialogResult.OK;
            this.Hide();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Hide();
        }

        private void selectedType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.okButton.Enabled = this.selectedType.SelectedIndex != -1;
        }
    }
}